package com.wish.wishsystem_management.service.impl;

import com.wish.wishsystem_management.entity.SysRole;
import com.wish.wishsystem_management.entity.SysRoleMenu;
import com.wish.wishsystem_management.mapper.SysRoleMapper;
import com.wish.wishsystem_management.service.ISysRoleDeptService;
import com.wish.wishsystem_management.service.ISysRoleMenuService;
import com.wish.wishsystem_management.service.ISysRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import dto.systemDto.RoleDto;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * <p>
 * 角色 服务实现类
 * </p>
 *
 * @author zhaiwj
 * @since 2021-03-22
 */
@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements ISysRoleService {

    @Autowired
    private ISysRoleMenuService iSysRoleMenuService;
    @Autowired
    private ISysRoleDeptService iSysRoleDeptService;

    //    列表
    @Override
    public List<SysRole> findAll(RoleDto roleDto) {
        SysRole sysRole = new SysRole();
        BeanUtils.copyProperties(roleDto, sysRole);

        return baseMapper.findAll(sysRole);
    }

    //    保存角色
    @Override
    public void saveRole(RoleDto roleDto) {
        roleDto.setCreateTime(new Date());
        SysRole sysRole = new SysRole();
        BeanUtils.copyProperties(roleDto, sysRole);
        baseMapper.insert(sysRole);


        //保存角色与菜单关系
        if (sysRole.getMenuIdList() != null && sysRole.getMenuIdList().size() > 0) {
            for (Long menuId : sysRole.getMenuIdList()) {
                SysRoleMenu sysRoleMenu = new SysRoleMenu();
                sysRoleMenu.setRoleId(sysRole.getRoleId());
                sysRoleMenu.setMenuId(menuId);
                iSysRoleMenuService.save(sysRoleMenu);
            }
        }
        // 保存角色与部门关系
        iSysRoleDeptService.saveDeptRole(sysRole);
    }

    /**
     * 删除角色
     */
    @Override
    public void deleteRoleById(Long roleId) {
        //先删除两个中间表
        iSysRoleMenuService.deleteByRoleId(roleId);
        iSysRoleDeptService.deleteByRoleId(roleId);
        //删除角色
        baseMapper.deleteById(roleId);
    }

    //根据角色id查询用户
    @Override
    public int queryTotalById(Long roleId) {
        return baseMapper.queryTotalById(roleId);
    }

    /**
     * 修改
     *
     * @param roleDto
     */
    @Override
    public void updateRole(RoleDto roleDto) {

        roleDto.setCreateTime(new Date());
        SysRole sysRole = new SysRole();
        BeanUtils.copyProperties(roleDto, sysRole);

        //修改角色
        this.updateById(sysRole);
        baseMapper.updateById(sysRole);

        //修改角色和菜单关系
        iSysRoleMenuService.updateRoleMenu(sysRole.getRoleId(), sysRole.getMenuIdList());

        //修改角色和部门的关系
        iSysRoleDeptService.updateRoleDept(sysRole);
    }

    @Override
    public List<SysRole> queryList2(Long deptId, Integer id) {
        return baseMapper.queryList2(deptId, id);
    }
}
